#include "carel-device-ui/main_ui.h"


int z3Ext_CreateBindingEntryWithUnicast(uint16_t clusterId,
										EmberEUI64 dstEui,
										uint8_t sep,
										uint8_t dep){
	EmberBindingTableEntry entry;
	entry.clusterId = clusterId;
	memcpy(entry.identifier, dstEui, 8);
	entry.local = sep;
	entry.remote = dep;
	entry.type = EMBER_UNICAST_BINDING;
	entry.networkIndex = emberGetCurrentNetwork();

	bool _full = true;
	EmberStatus _status;
	EmberBindingTableEntry _entry;
	uint8_t index;
	for(index = 0; index < EMBER_BINDING_TABLE_SIZE; index ++){
		_status = emberGetBinding(index, &_entry);
		if(_status == EMBER_SUCCESS && _entry.type == EMBER_UNUSED_BINDING){
			_full = false;
			break;
		}
	}

	if(!_full){
		return emberSetBinding(index, &entry)?-1:0;
	}else{
		UTILS_PRINT(UPM_EXT_BIND, "Error! binding table is full.\n");
		return -1;
	}
}

int z3Ext_GetBindingTableSizeInused(void){

	int ret = 0;
	EmberStatus _status;
	EmberBindingTableEntry _entry;
	uint8_t index;
	for(index = 0; index < EMBER_BINDING_TABLE_SIZE; index ++){
		_status = emberGetBinding(index, &_entry);
		if(_status == EMBER_SUCCESS && _entry.type != EMBER_UNUSED_BINDING){
			ret ++;
		}
	}

	return ret;
}

